package com.lamb.script.core.utils;

import java.io.PrintStream;

/**
 * console.info.
 *
 * @author lamb
 * @date 2017/3/22
 */
public class ScriptConsole {

    /**
     * 输出.
     */
    public final PrintStream out;

    /**
     * 错误输出.
     */
    public final PrintStream err;

    public ScriptConsole(PrintStream out, PrintStream err) {
        this.out = out;
        this.err = err;
    }

    public void debug(Object message) {
        log("DEBUG", message);
    }

    public void info(Object message) {
        log("INFO", message);
    }

    public void log(Object message) {
        log("LOG", message);
    }

    public void warn(Object message) {
        log("WARN", message);
    }

    public void error(Object message) {
        log("ERROR", message);
    }

    public void exception(Throwable e) {
        e.printStackTrace(err);
    }

    private void log(String level, Object message) {
        if ("ERROR".equals(level)) {
            this.err.println(String.valueOf(message));
            return;
        }
        this.out.println(String.valueOf(message));
    }

    /**
     * 刷新缓存.
     */
    public void flush() {
        out.flush();
        err.flush();
    }

    public void clear() {
        out.close();
        err.close();
    }
}
